﻿using Newtonsoft.Json;
using SpiderStudy.Utils.Currency;
using SpiderStudy.Utils.Html;
using System;
using System.Collections.Generic;
using static SpiderStudy.WebSite.ACGFI.ACGFIStaticValue;

namespace SpiderStudy.WebSite.ACGFI
{
    internal class ACGFIScanner
    {
        /// <summary>
        ///  保存指定页的数据，训练标题
        /// </summary>
        /// <param name="StartPage"></param>
        /// <param name="EndPage"></param>
        public void SaveTitleList(int StartPage, int EndPage)
        {
            List<string> TitleList = new List<string>();
            foreach (ArticleListInof Single in GetArticleList(StartPage, EndPage))
            {
                TitleList.Add(Single.Title);
            }

            new WebSiteHelper().SaveSpiderData("A区标题数据.txt", "Acgfi\\Title", TitleList);
        }

        /// <summary>
        /// 获取文章列表，返回对象ArticleListInof  包含标题和链接
        /// </summary>
        /// <param name="StartPage"></param>
        /// <param name="EndPage"></param>
        /// <returns></returns>
        public List<ArticleListInof> GetArticleList(int StartPage, int EndPage)
        {
            List<ArticleListInof> TempInfo = new List<ArticleListInof>();
            for (; StartPage < EndPage; StartPage++)
            {
                Console.WriteLine("正在扫描{0}页", StartPage);
                string Shtml = new WebHelper().HttpPost(new ACGFIStaticValue().ArticleListAPI1, string.Format("type=index&paged={0}", StartPage));
                Console.WriteLine(Shtml);

                try
                {
                    ArticleRoot JsonInfo = JsonConvert.DeserializeObject<ArticleRoot>(Shtml);

                    String AtrileCardHtml = JsonInfo.msg.Replace("\\", "");

                    List<string> ArticleInfoList = HtmlHelper.GetHtmlValueList(AtrileCardHtml, "//div[@class='post-info pos-r pd10 post-side']");
                    foreach (string Line in ArticleInfoList)
                    {
                        string TempTitle = HtmlHelper.GetFirstValue(HtmlHelper.GetTextValueList(Line, "//h2"));
                        string TempLink = HtmlHelper.GetFirstValue(HtmlHelper.GetLinkVlaueList(Line, "//h2/a"));
                        TempInfo.Add(new ArticleListInof { Title = TempTitle, ArticleUrl = TempLink });
                        Console.WriteLine(TempTitle);
                        Console.WriteLine(TempLink);
                    }
                    //post-info pos-r pd10 post-side
                }
                catch (Exception ex)
                {
                    PrintLog.Log(ex);
                }
            }

            return TempInfo;
        }
    }
}